<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:ModuleSwitchFilter.php,2010/05/31 15:42:38 
 * 
 **************************************************************************/
 
 
 
/**
 * @file ModuleSwitchFilter.php
 * @author huqingping
 * @date 2010/05/31 15:42:38
 * @version 1.0 
 * @brief 
 *  
 **/

/**
 * 框架功能开关
 */ 
class ModuleSwitchFilter implements IFilter
{
	function execute(WebApp $app)
	{
		//关闭提交功能
		$this->processPost($app);
		//关闭某个模块功能
		$this->processMod($app);
	}

	private function processPost($app)
	{
		$postEnable = Conf::get('hapn.switch.post',true);
		if (!$postEnable) {
			if ($app->request->method != 'GET') {
				throw new Exception('hapn.u_power');
			}
		}
	}

	private function processMod($app)
	{
		$disableapp = Conf::get('hapn.switch.disableapp');
		if ($disableapp) {
			$userip = $app->request->userip;
			foreach($disableapp as $appname=>$arrip) {
				$prefix = '/'.$appname;
				if (strncmp($prefix,$app->request->url, strlen($prefix)) === 0) {
					//如果模块被关闭		
					foreach($arrip as $ipprefix) {
						if (strncmp($ipprefix,$userip,strlen($ipprefix)) === 0) {
							//白名单ip
							continue 2;
						}
					}
					throw new Exception("hapn.u_modoff mod=$appname ip=$userip");
				}
			}
		}
	}
}





/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
